package com.huatai.bpmn.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.commom.dto.PageDTO;
import com.huatai.bpmn.commom.util.BaseCommService;
import com.huatai.bpmn.entity.YYFormEntity;
import com.huatai.bpmn.mapper.YYFormMapper;
import com.huatai.bpmn.service.YYFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  表单
 * </p>
 *
 * @author yxy
 * @since 2023-07-07
 */
@Service
public class YYFormServiceImpl extends ServiceImpl<YYFormMapper, YYFormEntity> implements YYFormService {

	@Autowired
	private BaseCommService baseCommService;

	/**
	 * 表单分页查询
	 * @param data
	 * @return
	 */
	@Override
	public IPage page(JSONObject data) {
		data.remove("sk_equal_routine");
		IPage iPage = new PageDTO().buildPage(data);

		QueryWrapper queryWrapper = (QueryWrapper) baseCommService.buildQW(data).orderByAsc("sort");

		IPage page = this.page(iPage, queryWrapper);
		return page;
	}

	public YYFormEntity findByPlace(String place){
		YYFormEntity formEntity = this.getOne(new LambdaQueryWrapper<>(YYFormEntity.class).eq(YYFormEntity::getPlace, place));
		return formEntity;
	}

	/**
	 * 表单外部参数修改
	 * @param data
	 * @param place
	 * @return
	 */
	@Override
	public boolean updateExternalParameter(JSONObject data, String place) {
		JSONArray json = data.getJSONArray("data");
		YYFormEntity formEntity = this.findByPlace(place);
//		formEntity.setExternalParameter(json.toJSONString());
		boolean flag = this.updateById(formEntity);
		return flag;
	}

	@Override
	public YYFormEntity getByPlace(String place) {

		return this.getOne(new LambdaQueryWrapper<>(YYFormEntity.class).eq(YYFormEntity::getPlace,place));
	}
}
